<template>
  <div class="comment">
    <div class="addcomment" v-show="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="handlerFocus" />
      <!-- 点击跳转到评论详情页 -->
      <span
        class="comment"
        @click="$router.push({ path: '/commentList/' + articaldetail.id })"
      >
        <i class="iconfont iconpinglun-"></i>
        <em>{{ articaldetail.comment_length }}</em>
      </span>
      <i
        class="iconfont iconshoucang"
        @click="handlerstar"
        :class="{ star: articaldetail.has_star }"
      ></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus">
      <textarea ref="commtext" rows="5" class="commtext"></textarea>
      <div>
        <span @click="sendComment">发 送</span>
        <span @click="cancelComment">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
// 引用封装发表评论api
import { post_star, send_comment } from "@/apis/article";
export default {
  props: {
    articaldetail: {
      requires: true,
    },
    commentObj: {
      default: null,
    },
  },
  data() {
    return {
      isFocus: false,
    };
  },
  watch: {
    commentObj() {
      // 监听，有值就弹框
      if (this.commentObj) {
        this.isFocus = true;
        setTimeout(() => {
          this.$refs.commtext.focus();
        }, 10);
      }
    },
    // isFocus() {
    //   if (!this.isFocus) {
    //     this.$emit("changeObj");
    //   }
    // },
  },
  methods: {
    // 按取消文本域隐藏
    cancelComment() {
      this.isFocus = false;
      // 在这里传给父组件，我要改变传的值obj
      this.$emit("changeObj");
    },
    //发送评论按钮
    async sendComment() {
      // 非空判断
      if (this.$refs.commtext.value.trim() == "") {
        this.$toast.fail("请输入评论");
        return;
      }
      // console.log(this.articaldetail.id);
      let params = {
        content: this.$refs.commtext.value,
      };
      // 当有数据传入
      if (this.commentObj) {
        params.parent_id = this.commentObj.id;
      }
      // console.log(params);
      let res = await send_comment(this.articaldetail.id, params);
      // console.log(res);
      if (res.data.message == "评论发布成功") {
        // 提示信息
        this.$toast.success(res.data.message);
        // 清空文本域
        this.$refs.commtext.value = "";
        this.isFocus = false;
        // 发一个事件，让评论列表刷新，就是子传父
        this.$emit("refresh");
      } else {
        this.$toast.fail(res.data.message);
      }
    },

    // 加一个setTimeout就可以解决文本域的获取焦点
    handlerFocus() {
      this.isFocus = !this.isFocus;
      setTimeout(() => {
        this.$refs.commtext.focus();
      }, 10);
      // document.querySelector(".commtext").focus();
    },

    // 不需要在父组件处理，只要在子组件处理
    // 点击文章收藏
    async handlerstar() {
      let res = await post_star(this.articaldetail.id);
      // console.log(res);
      this.articaldetail.has_star = !this.articaldetail.has_star;
      this.$toast.success(res.data.message);
    },
  },
};
</script>

<style lang="less" scoped>
.inputcomment {
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
    margin: 10px 0;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: fixed;
  bottom: 0;
  left: 0;
  background-color: #fff;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  .star {
    color: #ffd102;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>